﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTS.Model;
using System.Data.SqlClient;
using BTS.DAL;
using System.Data;
using pagination;
using System.Data.Common;

namespace BTS.BLL
{
    public class BlogBLL
    {
        private BlogDAL dal = new BlogDAL();

        /// <summary>
        /// 获取博文列表
        /// </summary>
        /// <param name="top">前几行，top num</param>
        /// <param name="sqlwhere">条件，and ...</param>
        /// <param name="orderby">排序</param>
        /// <param name="sqlp">参数</param>
        /// <returns></returns>
        public List<Blog> GetBlogs(string top, string sqlwhere, string orderby, params SqlParameter[] sqlp)
        {
            string sql = string.Format("select {0} * from blog where 1=1 {1} {2}", top, sqlwhere, orderby);
            List<Blog> list = dal.GetList<Blog>("", "", CommandType.Text, sql, sqlp);
            return list;
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <typeparam name="T">范型，不指定具体对象</typeparam>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="strSql">执行语句</param>
        /// <returns>范型集合</returns>
        public List<Blog> GetList(string pageSize, string current, string sqlwhere, string orderby, ref PageStructure ps)
        {
            SqlParameter[] objParams ={
				new SqlParameter("@table","[blog]"),
				new SqlParameter("@field", "*"),
				new SqlParameter("@where",sqlwhere),
				new SqlParameter("@pk","id"),
				new SqlParameter("@order",orderby),
				new SqlParameter("@pSize", pageSize),
				new SqlParameter("@pCurrent", current)
			};
            List<Blog> list = dal.GetList<Blog>("", "", ref ps, objParams);
            ps.CurrPage = Convert.ToInt16(current);
            return list;
        }

        /// <summary>
        /// 获得博文对象
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <param name="orderby"></param>
        /// <param name="sqlp"></param>
        /// <returns></returns>
        public Blog GetBlog(string sqlwhere, string orderby, params SqlParameter[] sqlp)
        {
            string sql = string.Format("select top 1 * from blog where 1=1 {0} {1}", sqlwhere, orderby);
            List<Blog> list = dal.GetList<Blog>("", "", CommandType.Text, sql, sqlp);
            if (list.Count > 0)
                return list[0];
            else
                return null;
        }

        /// <summary>
        /// 添加博文
        /// </summary>
        /// <param name="blog"></param>
        /// <returns></returns>
        public bool AddBlog(Blog blog)
        {
            bool b = true;
            string sql = string.Format("insert into blog values('{0}','{1}',{2},{3},'{4}',{5},'{6}','{7}')", blog.Id, blog.Title, blog.Wtime, blog.Utime, blog.Remarks, blog.Clicks, blog.Cid, blog.Introduction);
            b = dal.AddOrUpdateOrDelete("", "", CommandType.Text, sql, null);
            return b;
        }

        /// <summary>
        /// 删除博文
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteBlog(string id)
        {
            bool b = true;
            SqlParameter[] ps = new SqlParameter[] { 
                new SqlParameter("@id",id)
            };
            string sql = "delete from blog where id=@id";
            b = dal.AddOrUpdateOrDelete("", "", CommandType.Text, sql, ps);
            return b;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="indexs"></param>
        /// <returns></returns>
        public bool DeleteBlogs(string indexs)
        {
            bool b = true;
            string sql = "delete from blog where id in (" + indexs + ")";
            b = dal.AddOrUpdateOrDelete("", "", CommandType.Text, sql, null);
            return b;
        }

        /// <summary>
        /// 修改博文信息
        /// </summary>
        /// <param name="blog"></param>
        /// <returns></returns>
        public bool UpdateBlog(Blog blog)
        {
            bool b = true;
            SqlParameter[] ps = new SqlParameter[] { 
                new SqlParameter("@id",blog.Id),
                new SqlParameter("@title",blog.Title),
                new SqlParameter("@utime",blog.Utime),
                new SqlParameter("@remarks",blog.Remarks),
                new SqlParameter("@introduction",blog.Introduction)
            };
            string sql = "update blog set title=@title,utime=@utime,remarks=@remarks,introduction=@introduction where id=@id";
            b = dal.AddOrUpdateOrDelete("", "", CommandType.Text, sql, ps);
            return b;
        }

        /// <summary>
        /// 增加阅读量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateClick(string id)
        {
            bool b = true;
            SqlParameter[] ps = new SqlParameter[] { 
                new SqlParameter("@id",id)
            };
            string sql = "update blog set clicks=clicks+1 where id=@id";
            b = dal.AddOrUpdateOrDelete("", "", CommandType.Text, sql, ps);
            return b;
        }

        /// <summary>
        /// 统计博文数量
        /// </summary>
        /// <param name="sqlwhere"></param>
        /// <returns></returns>
        public int CountBlogs(string sqlwhere)
        {
            string sql = "select count(1) from blog where 1=1 " + sqlwhere;
            object obj = dal.ExecuteScalar("", "", CommandType.Text, sql, null);
            if (obj != null && obj != DBNull.Value)
                return Convert.ToInt16(obj);
            else
                return 0;
        }
    }
}
